package com.t.bricks.bussiness.db.dao.guest.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.guest.GuestDao;
import com.t.bricks.bussiness.db.mapper.guest.GuestMapper;
import com.t.bricks.bussiness.model.guest.Guest;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class GuestDaoImpl implements GuestDao {
	
	@Autowired
	private GuestMapper GuestMapper;

	/**
	 * 根据编号判断数据是否存在
	 * @Param sDbAndTable
	 * @Param sId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity hasById(String sDbAndTable, String sId) {
		try {
			int i = GuestMapper.hasById(sDbAndTable, sId);
			return new MsgEmity(true, "执行成功!", i > 0);
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改时发生异常!", 7002);
		}
	}

	/**
	 * 根据编号查询数据
	 * @Param sDbAndTable
	 * @Param sId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findById(String sDbAndTable, String sId) {
		try {
			Guest guest = GuestMapper.findById(sDbAndTable, sId);
			if (null != guest) {
				return new MsgEmity(true, "执行成功!", guest);
			}

			return new MsgEmity(false, "执行成功,但没有数据!", 7002);
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改时发生异常!", 7003);
		}
	}

	/**
	 * 修改密码
	 * @Param sDbAndTable
	 * @param sId
	 * @param sOldPass
	 * @param sNewPass
	 * @param iVersion
	 * @param sModifieder
	 * @return
	 */
	@Override
	public MsgEmity editPass(String sDbAndTable, String sId, String sOldPass,
			String sNewPass, int iVersion, String sModifieder) {
		try {
			int i = GuestMapper.editPass(sDbAndTable, sId, sOldPass, sNewPass, iVersion, sModifieder);
			if (i < 1) {
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改失败!", 7001);
			}
		} catch (Exception e) {
			Log.error("修改时发生异常:", e.getMessage());
			return new MsgEmity(false, "修改时发生异常!", 7002);
		}
		
		return new MsgEmity(true, "修改成功!", 7999);
	}
	
	/**
	 * 根据账户编号设置账户类型
	 * @Param sDbAndTable
	 * @param sId 账户编号
	 * @param sType 账户类型
	 * @return
	 */
	@Override
	public MsgEmity editUserType(String sDbAndTable, String sId, String sType) {
		try {
			int i = GuestMapper.editUserType(sDbAndTable, sId, sType);
			if (i < 1) {
				int iCount = GuestMapper.hasById(sDbAndTable, sId);
				if (iCount < 1) {
					return new MsgEmity(false, "指定账户不存在!", 7001);
				}
				
				Log.error("设置未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "设置失败!", 7002);
			}
		} catch (Exception e) {
			Log.error("设置时发生异常:", e.getMessage());
			return new MsgEmity(false, "设置时发生异常!", 7003);
		}
		
		return new MsgEmity(true, "设置成功!", 7999);
	}

}
